<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="结果代码" prop="errcode">
      <el-input v-model="dataForm.errcode" placeholder="结果代码"></el-input>
    </el-form-item>
    <el-form-item label="结果代码宏定义" prop="errmacro">
      <el-input v-model="dataForm.errmacro" placeholder="结果代码宏定义"></el-input>
    </el-form-item>
    <el-form-item label="结果代码说明" prop="errdesc">
      <el-input v-model="dataForm.errdesc" placeholder="结果代码说明"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          seqno: 0,
          errcode: '',
          errmacro: '',
          errdesc: ''
        },
        dataRule: {
          errcode: [
            { required: true, message: '结果代码不能为空', trigger: 'blur' }
          ],
          errmacro: [
            { required: true, message: '结果代码宏定义不能为空', trigger: 'blur' }
          ],
          errdesc: [
            { required: true, message: '结果代码说明不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.seqno = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.seqno) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bdcerrmsg/info/${this.dataForm.seqno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.errcode = data.bdcErrmsg.errcode
                this.dataForm.errmacro = data.bdcErrmsg.errmacro
                this.dataForm.errdesc = data.bdcErrmsg.errdesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bdcerrmsg/${!this.dataForm.seqno ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'seqno': this.dataForm.seqno || undefined,
                'errcode': this.dataForm.errcode,
                'errmacro': this.dataForm.errmacro,
                'errdesc': this.dataForm.errdesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
